import java.util.Map;

public class CC19 {
    public int minCut(String s) {
        // write code here
        int len = s.length();
        if(len == 0) {
            return 0;
        }
        if(isPal(s,0,len - 1)){
            return 0;
        }
        int[] arr = new int[len + 1];
        //先初始化
        for (int i = 1; i <= len; i++) {
            arr[i] = i - 1;
        }
        for (int i = 2; i <= len; i++) {
            if(isPal(s,0,i - 1)) {
                arr[i] = 0;
                continue;
            }
            for (int j = 1; j < i; j++) {
                if(isPal(s,j, i - 1)) {
                    arr[i] = Math.min(arr[i],arr[j] + 1);
                }
            }
        }
        return arr[len];
    }

    private boolean isPal(String s, int start, int end) {
       while(start < end) {
           if(!s.substring(start,start + 1).equals(s.substring(end,end + 1))) {
               return false;
           }
           start ++;
           end --;
       }
       return true;
    }
}
